//
//  MainTabBar.swift
//  weibo
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit
//mainTabBar 即支持手码创建 又支持xib/sb创建
class MainTabBar: UITabBar {
//UIView的默认的构造方法
    //重写父类的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButon)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置所有子控件的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //修改所有tabbarbutton的frmae + 将中间位置空出来
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        //遍历所有的子视图 修改大小
        //定义索引系数
        var index: CGFloat = 0
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
            subView.frame = CGRectOffset(rect , index * w, 0)
                index += (index == 1 ? 2 : 1)
            }
        }
        //设置加号按钮的位置
        composeButon.frame = CGRectOffset(rect, 2 * w, 0)
    }
    //懒加载 加号按钮
    lazy var composeButon: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
}